// src/stores/grabTask.js
import { defineStore } from 'pinia'
import { grabTaskApi } from '@/api/grabTask'
import {crawlerTaskApi} from "@/api/crawlerTask.js";

export const useGrabTaskStore = defineStore('grabTask', {
    state: () => ({
        tasks: [],
        loading: false,
        currentTaskLogs: []
    }),

    actions: {
        async fetchTasks() {
            this.loading = true
            try {
                const response = await grabTaskApi.getTasks()
                this.tasks = response.data || []
            } finally {
                this.loading = false
            }
        },

        async createTask(task) {
            const response = await grabTaskApi.createTask(task)
            await this.fetchTasks()
            return response
        },

        async updateTask(id, task) {
            const response = await grabTaskApi.updateTask(id, task)
            await this.fetchTasks()
            return response
        },

        async deleteTask(id) {
            const response = await grabTaskApi.deleteTask(id)
            await this.fetchTasks()
            return response
        },

        async executeTask(id) {
            const response = await grabTaskApi.executeTask(id)
            await this.fetchTasks()
            return response
        },
    }
})